<template>
  <div class="user">
    <div class="userBox" v-if="usershow">
      <div class="userBox-top">
        <h1>用户管理</h1>
        <button @click="addUser">添加用户</button>
      </div>
      <div class="table">
        <div class="table-top">
          <h2>用户信息</h2>
          <div>
            <input type="text" v-model="queryinfo.userName" placeholder="请输入用户名称" />
            <button @click="searchUser">搜索</button>
          </div>
        </div>
        <table>
          <tbody>
            <tr>
              <th>序号</th>
              <th>宇宙id</th>
              <th>用户名</th>
              <th>密码</th>
              <th>操作</th>
            </tr>
            <tr v-for="(item,index) in userList" :key="item.id">
              <th>{{index+1}}</th>
              <th>{{item._id}}</th>
              <th>{{item.username}}</th>
              <th>{{item.password}}</th>
              <th>
                <button style="backgroundColor:yellow" @click="updateUser(index+1)">修改</button>
                <button style="backgroundColor:red" @click="deleteUser(item._id)">删除</button>
              </th>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
    <updateuser
      v-if="!usershow"
      :updateuser="oldList"
      @updateUsersignOut="updateUsersignOut1"
      @updateUserSubmit="updateUserSubmit1"
    ></updateuser>
  </div>
</template>

<script>
import updateuser from "@/components/updateuser.vue";
export default {
  name: "user",
  props: [],
  components: {
    updateuser
  },
  data() {
    return {
      userList: [],
      oldList: {
        username: "",
        password: ""
      },
      usershow: true,
      queryinfo: {
        id: "",
        userName: ""
      }
    };
  },
  //方法 函数写这里
  methods: {
    async getUserList() {
      const res = await this.$http.get("http://127.0.0.1:3003/user");
      this.userList = res.data.data;
    },
    async searchUser() {
      if (this.queryinfo.userName == "") {
        return;
      }
      const res = await this.$http.get("http://127.0.0.1:3003/searchUser", {
        params: this.queryinfo
      });
      this.userList = res.data.data;
    },
    addUser() {
      this.$router.push("/home/adduser");
    },
    updateUser(id) {
      this.usershow = !this.usershow;
      this.oldList = this.userList[id - 1];
    },
    async deleteUser(id) {
      console.log(id);
      var a = confirm("操作将永久删除用户确定吗?");
      if (!a) {
        this.$message.info("算了先不删");
        return;
      }
      const res = await this.$http.get("http://127.0.0.1:3003/deleteUser", {
        params: id
      });
      this.userList = res.data;
      this.getUserList();
    },

    updateUsersignOut1() {
      this.usershow = true;
    },
    async updateUserSubmit1() {
      if (this.oldList.username == "" || this.oldList.password == "") {
        return;
      }
      const res = await this.$http.post("http://127.0.0.1:3003/updateUser", {
        params: this.oldList
      });
      if (res.data.code == 200) {
        this.usershow = true;
      }
    }
  },
  //计算属性
  computed: {},
  //侦听器
  watch: {},
  //过滤器
  filters: {},
  //以下是生命周期
  //组件创建之前
  beforeCreate() {},
  //组件创建之后
  created() {
    this.getUserList();
  },
  //页面渲染之前
  beforeMount() {},
  //页面渲染之后
  mounted() {},
  //页面视图数据更新之前
  beforeUpdate() {},
  //页面视图数据更新之后
  updated() {},
  //页面销毁之前
  beforeDestroy() {},
  //页面销毁之后
  destroyed() {}
};
</script>


<style lang="scss"  scoped>
.user {
  width: 100%;
  height: 100%;
}
.userBox {
  margin-top: 10px;
  margin-left: 10px;
  h1 {
    display: inline-block;
    margin-right: 10px;
  }
  .userBox-top button {
    width: 80px;
    height: 24px;
    line-height: 24px;
    background-color: skyblue;
  }
}
.table {
  width: 100%;
  margin-top: 20px;
  background-color: white;
  box-shadow: 1px -1px black;
  padding-left: 5px;
  .table-top {
    height: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    button {
      background-color: skyblue;
      margin-right: 400px;
    }
  }
  tbody tr {
    margin-top: 10px;
    font-size: 16px;
    th {
      padding: 10px 40px;
      button {
        width: 60px;
        height: 30px;
      }
    }
  }
}
</style>